package com.c.yinyuezhushou.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.c.yinyuezhushou.Adapter.SetupRecAdapter;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.R;
import com.leon.lfilepickerlibrary.LFilePicker;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;

public class SetupFragment extends Fragment {
    private View rootView;
    private RecyclerView recyclerView;
    private MyApplication myApplication;
    private TextView textView;//路径
    private int REQUESTCODE_FROM_ACTIVITY = 1000;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_setup, container, false);
        recyclerView = rootView.findViewById(R.id.fs_rec1);
        myApplication =(MyApplication) getActivity().getApplicationContext();
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(new SetupRecAdapter(myApplication,this));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUESTCODE_FROM_ACTIVITY) {
                String path = data.getStringExtra("path")+"/";
                myApplication.getSetup().setPath(path);
                textView.setText(path);
            }
        }
    }

    //文件选择器
    public void FilePath(TextView textView){
        this.textView = textView;
        new LFilePicker()
                .withSupportFragment(this)
                .withRequestCode(REQUESTCODE_FROM_ACTIVITY)
                .withChooseMode(false)//设置选择文件夹
                .withStartPath(myApplication.getSetup().getPath())//指定初始显示路径
                .start();
    }
}
